<?php

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;

if (!function_exists('isMobile')) {
    /**
     * 检测是否是移动端浏览器
     *
     * @return bool
     */
    function isMobile()
    {
        $mobiles = ['Mobile', 'iPad', 'Android', 'iPhone', 'Silk', 'Kindle', 'BlackBerry', 'Opera Mini', 'Opera Mobi'];

        return Str::contains(Request::server('HTTP_USER_AGENT'), $mobiles);
    }
}
if (!function_exists('isWeChat')) {
    /**
     * 检测是否是微信浏览器
     *
     * @return bool
     */
    function isWeChat()
    {
        return Str::contains(Request::server('HTTP_USER_AGENT'), ['MicroMessenger']);
    }
}
if (!function_exists('convertFileSize')) {
    /**
     * 转换文件大小
     *
     * @param int $size
     *
     * @return string
     */
    function convertFileSize($size)
    {
        if ($size >= pow(2, 40)) {
            $size = round($size / pow(2, 40), 2);
            $dw = "TB";
        } elseif ($size >= pow(2, 30)) {
            $size = round($size / pow(2, 30), 2);
            $dw = "GB";
        } elseif ($size >= pow(2, 20)) {
            $size = round($size / pow(2, 20), 2);
            $dw = "MB";
        } elseif ($size >= pow(2, 10)) {
            $size = round($size / pow(2, 10), 2);
            $dw = "KB";
        } else {
            $dw = "Bytes";
        }

        return $size . $dw;
    }
}
if (!function_exists('formatChineseName')) {
    /**
     * @param      $firstName
     * @param null $lastName
     *
     * @return string
     */
    function formatChineseName($firstName, $lastName = null)
    {
        $preg = chr(0xa1) . "-" . chr(0xff);;
        $first = preg_match("/[$preg]+/", $firstName, $mF);
        $last = preg_match("/[$preg]+/", $lastName, $mL);
        if ($first || $last) {
            return $lastName . $firstName;
        }

        return $firstName . ' ' . $lastName;
    }
}
if (!function_exists('toArray')) {
    /**
     * 转换为数组
     *
     * @param mixed|null $value
     *
     * @return array
     */
    function toArray($value = null)
    {
        $value = value($value);
        if (is_array($value)) {
            return $value;
        }
        if ($value instanceof Arrayable) {
            return $value->toArray();
        }

        return !empty($value) ? (array)$value : [];
    }
}
if (!function_exists('arrayToTree')) {
    /**
     * 转换为树形
     *
     * @param array       $data
     * @param string|null $p_id
     * @param string      $pIdKey
     * @param string      $idKey
     * @param string      $childrenKey
     *
     * @return array
     */
    function arrayToTree(array $data = [], $p_id = null, $pIdKey = 'p_id', $idKey = 'id', $childrenKey = 'children')
    {
        $branch = [];
        foreach ($data as $item) {
            if ($item[$pIdKey] == $p_id) {
                $item['checkArr'] = [
                    "type" => "0",
                    "checked" => "0",
                ];
                $children = arrayToTree($data, $item[$idKey], $pIdKey, $idKey, $childrenKey);
                if ($children) {
                    $item[$childrenKey] = $children;
                }
                $branch[] = $item;
            }
        }

        return $branch;
    }
}